<?php

namespace App\Task\Update;

use App\Model\UserCoin;
use App\Model\VideoCall;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 定时更新冻结账户金币
 * @Crontab(name="UpdateFreezeTask", rule="*\/10 * * * * *", callback="execute", memo="定时更新冻结账户金币", singleton=true)
 */
class UpdateFreezeTask
{
    public function execute()
    {
//        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $logs = VideoCall::getChatting();
        $userIds = [];
        foreach ($logs as $log) {
            $userIds[] = $log['user_id'];
            $userIds[] = $log['chat_user_id'];
        }
        $userIds = array_unique($userIds);

        //恢复冻结资金
        $where = [['freeze_coin', '>', 0]];
        $data = UserCoin::where($where)->whereNotIn('user_id', $userIds)->pluck('freeze_coin', 'user_id')->toArray();
        $ret = UserCoin::where($where)->whereNotIn('user_id', $userIds)->update(['freeze_coin' => 0]);
//        log_info(__CLASS__ . "==UpdateUsers:" . json_encode($data) . "==Result:{$ret}");
        return;
    }
}
